
/*
 * Question: Try to travel a binary tree in level order.
 */

#include <iostream>
#include <queue>
#include "bitree.h"
using namespace std;

class bitree : public _bitree<bitree, char>
{
};

static void visit(bitree *rt)
{
    cout << rt->key << " ";
}

void levelorder(bitree *rt)
{
    bitree *p;
    queue<bitree *> q;

    if(rt) q.push(rt);
    while(!q.empty()){
        p = q.front(); q.pop();
        visit(p);
        if(p->lchild)
            q.push(p->lchild);
        if( p->rchild )
            q.push(p->rchild);
    }
}

int main()
{
    bitree *rt;

    bitree_create<bitree,char>(rt, "ex47.txt");
    levelorder(rt);
    cout << endl;
    bitree_destory(rt);

    return 0;
}
